﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.MODEL.Statistics;

using System.Data;
using System.Data.Objects;
using System.Data.Common;
using System.Data.Entity;
using MINERP.DAL;
//using MINERP.MODEL.Statistics;
using MINERP.DAL.Statistics;
using MINERP.COM;
using MINERP.MODEL.FoundationModel;

namespace MINERP.BLL.Statistics
{
    public class StatisticsBLL
    {
        private DAL.Statistics.StatisticsDAL SSDAL = new MINERP.DAL.Statistics.StatisticsDAL();
        private MINERP.DAL.CommonDAL.OrderInfoDAL _oderDal = new MINERP.DAL.CommonDAL.OrderInfoDAL();
        #region 获取所有的商户信息
        /// <summary>
        /// 获取所有的商户信息
        /// </summary>
        /// <returns></returns>
        public List<MINERP.MODEL.Statistics.Shop_Info> GetShopList()
        {
            try
            {
                ObjectQuery<DbDataRecord> query = SSDAL.GetShopList();
                List<MINERP.MODEL.Statistics.Shop_Info> ListShop = new List<MINERP.MODEL.Statistics.Shop_Info>();
                foreach (DbDataRecord record in query)
                {
                    MINERP.MODEL.Statistics.Shop_Info s = new MINERP.MODEL.Statistics.Shop_Info();
                    s.ShopID = Convert.ToInt32(record["ShopId"]);
                    s.ShopName = record["shopName"].ToString();
                    ListShop.Add(s);
                }
                return ListShop.OrderBy(p => p.ShopName).ToList(); ;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 获取某商户，某段时间内所有的账单
        /// <summary>
        /// 获取某商户，某段时间内所有的账单
        /// </summary>
        /// <param name="shopId"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public List<Bill_Info> GetBillInfo(int shopId, DateTime startTime, DateTime endTime)
        {
            try
            {
                List<SHOP_Bill> bs = SSDAL.GetShopBill(shopId, startTime, endTime);
                List<Bill_Info> bills = new List<Bill_Info>();
                foreach (SHOP_Bill b in bs)
                {
                    Bill_Info bill = new Bill_Info();
                    bill.IsChecked = true;
                    bill.BillID = b.BillId;
                    bill.BillOrderCode = b.BillOrderCode;
                    bill.CreateTime = b.CreateTime;
                    if (!b.ShopReference.IsLoaded)
                    {
                        b.ShopReference.Load();
                    }
                    bill.ShopID = b.Shop.ShopId;
                    bill.ShopName = b.Shop.shopName;
                    bills.Add(bill);
                }
                return bills;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 获取某商户，某段时间内未结单的订单
        /// <summary>
        /// 获取某商户，某段时间内未结单的订单
        /// </summary>
        /// <param name="shopId"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public List<Bill_Info> GetBillInfoNoPay(int shopId, DateTime startTime, DateTime endTime)
        {
            try
            {
                int status = (int)COM.ShopBillStatus.UnCheckOut;                                    //表示还未结单
                List<SHOP_Bill> bs = SSDAL.GetShopBillNoPay(shopId, startTime, endTime, status);
                List<Bill_Info> bills = new List<Bill_Info>();
                foreach (SHOP_Bill b in bs)
                {
                    Bill_Info bill = new Bill_Info();
                    bill.IsChecked = true;
                    bill.BillID = b.BillId;
                    bill.BillOrderCode = b.BillOrderCode;
                    bill.CreateTime = b.CreateTime;
                    if (!b.ShopReference.IsLoaded)
                    {
                        b.ShopReference.Load();
                    }
                    bill.ShopID = b.Shop.ShopId;
                    bill.ShopName = b.Shop.shopName;
                    bills.Add(bill);
                }
                return bills;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 获取具体某个产品的金额
        /// <summary>
        /// 获取具体某个产品的金额
        /// </summary>
        /// <param name="billDetailId"></param>
        /// <returns></returns>
        public double GetPriceOfAOrderInfo(int billDetailId)
        {
            try
            {
                return SSDAL.GetPriceOfAOrderInfo(billDetailId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 获取一段时间内所有的产品金额统计
        public DataTable GetProductPrice(DateTime startTime, DateTime endTime)
        {
            try
            {
                List<BillDetailEntity> ListProductDetial = GetAllBillDetail(startTime, endTime);
                var group = ListProductDetial.GroupBy(p => p.ProductId);
                DataTable productPriceDT = MODEL.Statistics.BillDataTable.CreateProductPrice();
                foreach (var g in group)
                {
                    DataRow row = productPriceDT.NewRow();
                    int num = 0;
                    double price = 0;
                    double area = 0;
                    string pn = g.First().ProductName;
                    foreach (var gg in g)
                    {
                        num++;
                        price += gg.BillDetailPrice;
                        area += StatisticsDAL.GetAreaOfAOrderInfo(gg.OrderInfoCode);        //每个产品的总面积
                    }
                    row["ProductName"] = pn;
                    row["ProductNum"] = num;
                    row["ProductArea"] = area;                                              //这种产品的总面积
                    row["Price"] = price;
                    productPriceDT.Rows.Add(row);
                }
                return productPriceDT;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一段时间内所有的产品金额明细
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public List<MODEL.Statistics.BillDetailEntity> GetAllBillDetail(DateTime startTime, DateTime endTime)
        {
            try
            {
                List<Bill_Detail> ListBillDetail = SSDAL.GetListBillDetail(startTime, endTime);
                List<MODEL.Statistics.BillDetailEntity> ListBillProduct = new List<BillDetailEntity>();
                foreach (Bill_Detail bd in ListBillDetail)
                {
                    MODEL.Statistics.BillDetailEntity bill = new BillDetailEntity();
                    bill.BillDetailId = bd.BillDetailId;
                    double price = SSDAL.GetPriceOfAOrderInfo(bd.BillDetailId);
                    bill.BillDetailPrice = price;
                    Tb_Product product = SSDAL.GetProductByOrderInfoCode(bd.OrderInfoCode);
                    if (product == null)
                        continue;
                    bill.OrderInfoCode = bd.OrderInfoCode;
                    bill.ProductId = product.ProuctId;
                    bill.ProductName = product.ProductName;
                    ListBillProduct.Add(bill);
                }
                return ListBillProduct;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        public List<MODEL.FoundationModel.SHOP_Bill_Info> GetBillDetails(List<int> billIds)
        {
            try
            {
                List<MODEL.FoundationModel.SHOP_Bill_Info> BillDetails = new List<MINERP.MODEL.FoundationModel.SHOP_Bill_Info>();
                foreach (int billId in billIds)
                {
                    MODEL.FoundationModel.SHOP_Bill_Info shopBill = new MINERP.MODEL.FoundationModel.SHOP_Bill_Info();
                    shopBill.List_Detail = new List<MINERP.MODEL.FoundationModel.Bill_Detail_Info>();
                    SHOP_Bill sb = SSDAL.GetBills(billId);
                    shopBill.BillId = sb.BillId;
                    shopBill.BillOrderCode = sb.BillOrderCode;
                    shopBill.CreateTime = sb.CreateTime;
                    //账单内的产品
                    List<Bill_Detail> billDetails = SSDAL.GetBillDetails(billId);
                    List<MODEL.FoundationModel.Bill_Detail_Info> DetailBills = new List<MINERP.MODEL.FoundationModel.Bill_Detail_Info>();
                    foreach (Bill_Detail bd in billDetails)
                    {
                        MODEL.FoundationModel.Bill_Detail_Info db = new MINERP.MODEL.FoundationModel.Bill_Detail_Info();
                        db.ListDetail_Detail = new List<MINERP.MODEL.FoundationModel.Bill_Detail_Detail_Info>();
                        db.BillDetailId = bd.BillDetailId;
                        //产品的详情
                        List<Bill_Detail_Detail> BillDDetails = SSDAL.GetBillDetailDetails(bd.BillDetailId);
                        List<MINERP.MODEL.FoundationModel.Bill_Detail_Detail_Info> DDetailBills = new List<MINERP.MODEL.FoundationModel.Bill_Detail_Detail_Info>();
                        foreach (Bill_Detail_Detail bdd in BillDDetails)
                        {
                            MINERP.MODEL.FoundationModel.Bill_Detail_Detail_Info ddb = new MINERP.MODEL.FoundationModel.Bill_Detail_Detail_Info();
                            ddb.Area = bdd.Area;
                            //ddb.Bill_Detail = db;
                            ddb.Bill_Detail_DetailId = bdd.Bill_Detail_DetailId;
                            ddb.Count = bdd.Count;
                            ddb.DetailName = bdd.DetailName;
                            ddb.Num = bdd.Num;
                            //string guiGe = bdd.Spec;
                            //int index = guiGe.IndexOf("*");
                            //double h = Convert.ToDouble(guiGe.Substring(0, index));
                            //double w = Convert.ToDouble(guiGe.Substring(index + 1));
                            //double h2, w2;
                            //h2 = h * 2.54;
                            //w2 = w * 2.54;

                            //ddb.Spec = h.ToString() + "×" + w.ToString() + "in" + "(" + h2.ToString() + "×" + w2.ToString() + "cm)";
                            ddb.Spec = bdd.Spec;
                            ddb.UnitPrice = bdd.UnitPrice;
                            //DDetailBills.Add(ddb);
                            db.ListDetail_Detail.Add(ddb);
                        }

                        //db.ListDetail_Detail = DDetailBills;
                        db.OrderInfoCode = bd.OrderInfoCode;
                        db.OrderProductName = bd.OrderProductName;
                        //try
                        //{
                        //    orderInfo oi = _oderDal.GetEntityByBarCode(bd.OrderInfoCode);
                        //    if (oi == null)
                        //    {

                        //    }

                        //    db.CtmName = oi.CtmName;
                        //}
                        //catch
                        //{
                        //    db.CtmName = bd.OrderInfoCode;
                        //}
                        orderInfo oi = _oderDal.GetEntityByBarCode(bd.OrderInfoCode);
                        if (oi != null)
                        {
                            db.CtmName = oi.CtmName;
                        }
                        else
                        {
                            orderInfo_BK oibk = _oderDal.GetEntityByBarCodeBK(bd.OrderInfoCode);
                            if (oibk != null)
                            {
                                db.CtmName = oibk.CtmName;
                            }
                            else
                            {
                                db.CtmName = bd.OrderInfoCode;
                            }
                        }

                        db.Shop_Bill = shopBill;
                        //DetailBills.Add(db);
                        shopBill.List_Detail.Add(db);
                    }
                    //shopBill.List_Detail = DetailBills;
                    MODEL.FoundationModel.Shop_Info si = new MINERP.MODEL.FoundationModel.Shop_Info();
                    //商户
                    if (!sb.ShopReference.IsLoaded)
                    {
                        sb.ShopReference.Load();
                    }
                    si.Address = sb.Shop.address;
                    si.MnemonicCode = sb.Shop.mnemonicCode;
                    si.ShopId = sb.Shop.ShopId;
                    si.ShopKeeper = sb.Shop.shopKeeper;
                    si.ShopName = sb.Shop.shopName;
                    si.Tel1 = sb.Shop.Tel1;
                    si.Tel2 = sb.Shop.Tel2;
                    //si.UnitPrice=sb.Shop.u
                    shopBill.Shop = si;
                    BillDetails.Add(shopBill);
                }
                return BillDetails;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 根据BillId得到一个MODEL实体
        /// </summary>
        /// <param name="billId"></param>
        /// <returns></returns>
        public MINERP.MODEL.FoundationModel.SHOP_Bill_Info GetItByShopBillId(int billId)
        {
            MINERP.MODEL.FoundationModel.SHOP_Bill_Info shopBill = new MINERP.MODEL.FoundationModel.SHOP_Bill_Info();
            shopBill.List_Detail = new List<MINERP.MODEL.FoundationModel.Bill_Detail_Info>();
            SHOP_Bill sb = SSDAL.GetBills(billId);
            shopBill.BillId = sb.BillId;
            shopBill.BillOrderCode = sb.BillOrderCode;
            shopBill.CreateTime = sb.CreateTime;
            //账单内的产品
            List<Bill_Detail> billDetails = SSDAL.GetBillDetails(billId);
            List<MODEL.FoundationModel.Bill_Detail_Info> DetailBills = new List<MINERP.MODEL.FoundationModel.Bill_Detail_Info>();
            foreach (Bill_Detail bd in billDetails)
            {
                MODEL.FoundationModel.Bill_Detail_Info db = new MINERP.MODEL.FoundationModel.Bill_Detail_Info();
                db.ListDetail_Detail = new List<MINERP.MODEL.FoundationModel.Bill_Detail_Detail_Info>();
                db.BillDetailId = bd.BillDetailId;
                //产品的详情
                List<Bill_Detail_Detail> BillDDetails = SSDAL.GetBillDetailDetails(bd.BillDetailId);
                List<MINERP.MODEL.FoundationModel.Bill_Detail_Detail_Info> DDetailBills = new List<MINERP.MODEL.FoundationModel.Bill_Detail_Detail_Info>();
                foreach (Bill_Detail_Detail bdd in BillDDetails)
                {
                    MINERP.MODEL.FoundationModel.Bill_Detail_Detail_Info ddb = new MINERP.MODEL.FoundationModel.Bill_Detail_Detail_Info();
                    ddb.Area = bdd.Area;
                    ddb.Bill_Detail_DetailId = bdd.Bill_Detail_DetailId;
                    ddb.Count = bdd.Count;
                    ddb.DetailName = bdd.DetailName;
                    ddb.Num = bdd.Num;
                    ddb.Spec = bdd.Spec;
                    ddb.UnitPrice = bdd.UnitPrice;
                    db.ListDetail_Detail.Add(ddb);
                }
                db.OrderInfoCode = bd.OrderInfoCode;
                db.OrderProductName = bd.OrderProductName;
                try
                {
                    orderInfo oi = _oderDal.GetEntityByBarCode(bd.OrderInfoCode);
                    db.CtmName = oi.CtmName;
                }
                catch
                {
                    db.CtmName = bd.OrderInfoCode;
                }


                db.Shop_Bill = shopBill;

                shopBill.List_Detail.Add(db);
            }
            MINERP.MODEL.FoundationModel.Shop_Info si = new MINERP.MODEL.FoundationModel.Shop_Info();
            //商户
            if (!sb.ShopReference.IsLoaded)
            {
                sb.ShopReference.Load();
            }
            si.Address = sb.Shop.address;
            si.MnemonicCode = sb.Shop.mnemonicCode;
            si.ShopId = sb.Shop.ShopId;
            si.ShopKeeper = sb.Shop.shopKeeper;
            si.ShopName = sb.Shop.shopName;
            si.Tel1 = sb.Shop.Tel1;
            si.Tel2 = sb.Shop.Tel2;
            shopBill.Shop = si;
            return shopBill;
        }

        #region 获取账单的详情(用List表示)
        /// <summary>
        /// 获取账单的所有信息(用List表示)
        /// </summary>
        /// <param name="bills"></param>
        /// <returns></returns>
        public List<MODEL.FoundationModel.SHOP_Bill_Info> GetShop_Bill_InfoFromBill_Info(List<Bill_Info> ListBill)
        {
            try
            {
                List<int> BillIds = new List<int>();
                foreach (Bill_Info b in ListBill)
                {
                    BillIds.Add(b.BillID);
                }
                List<MODEL.FoundationModel.SHOP_Bill_Info> ShopBillDetails = GetBillDetails(BillIds);
                return ShopBillDetails;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 将账单从List转化成DataSet,并将对应的XML文件写入内存
        /// <saummary>
        /// 将账单从List转化成DataSet
        /// </summary>
        /// <param name="ListBill"></param>
        /// <returns></returns>
        public DataSet GetBillInfoFromListToDataSet(List<MODEL.FoundationModel.SHOP_Bill_Info> ListBill, int status)
        {
            DataTable tableTitle = BillDataTable.CreateShopBillTitle();
            DataTable tableDetail = BillDataTable.CreateShopBillDetail();
            DataTable tableSubDetail = BillDataTable.CreateShopBillSubDetail();
            foreach (MODEL.FoundationModel.SHOP_Bill_Info sbinfo in ListBill)
            {
                DataRow titleRow = tableTitle.NewRow();
                titleRow["ShopName"] = sbinfo.Shop.ShopName;
                titleRow["Tel1"] = sbinfo.Shop.Tel1;
                titleRow["ShopId"] = sbinfo.Shop.ShopId;
                titleRow["BillId"] = sbinfo.BillId;
                titleRow["BillOrderCode"] = sbinfo.BillOrderCode;
                titleRow["BillCreateTime"] = sbinfo.CreateTime;
                tableTitle.Rows.Add(titleRow);




                List<MODEL.FoundationModel.Bill_Detail_Info> detailList = sbinfo.List_Detail.ToList();

                foreach (MODEL.FoundationModel.Bill_Detail_Info billDetail in detailList)
                {
                    DataRow row = tableDetail.NewRow();
                    row["BillId"] = sbinfo.BillId;
                    row["BillDetailId"] = billDetail.BillDetailId;
                    row["OrderInfoCode"] = billDetail.OrderInfoCode;
                    row["OrderProductName"] = billDetail.OrderProductName;
                    row["CtmName"] = billDetail.CtmName;
                    tableDetail.Rows.Add(row);
                    List<MODEL.FoundationModel.Bill_Detail_Detail_Info> subDetail = billDetail.ListDetail_Detail.ToList();
                    foreach (MODEL.FoundationModel.Bill_Detail_Detail_Info detail in subDetail)
                    {
                        DataRow rowSub = tableSubDetail.NewRow();
                        rowSub["DetailName"] = detail.DetailName;
                        rowSub["BillDetailId"] = billDetail.BillDetailId;
                        rowSub["Spec"] = detail.Spec;
                        rowSub["Num"] = detail.Num;
                        rowSub["UnitPrice"] = Convert.ToDouble(string.Format("{0:0.##}", detail.Count / detail.Num));                  //修改单价
                        if (status == 0)
                        {
                            rowSub["Count"] = -detail.Count;
                        }
                        else
                        {
                            rowSub["Count"] = detail.Count;
                        }
                        rowSub["Area"] = detail.Area;
                        tableSubDetail.Rows.Add(rowSub);
                    }
                }
            }
            tableTitle.TableName = "tableTitle";
            tableDetail.TableName = "tableDetail";
            tableSubDetail.TableName = "tableSubDetail";


            DataSet set = new DataSet();
            set.Tables.Add(tableTitle);
            set.Tables.Add(tableDetail);
            set.Tables.Add(tableSubDetail);

            set.WriteXml(@"C:\Windows\xmldata\PrintShopBill2.xml", XmlWriteMode.WriteSchema);
            return set;
        }
        #endregion
        #region 获取所有商户一段时间内未结单金额
        /// <summary>
        /// 获取所有商户一段时间内未结单金额
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public DataTable GetShopBillNoPay(DateTime startTime, DateTime endTime)
        {
            try
            {
                BillEditBLL bell = new BillEditBLL();
                List<Shop> ListShop = SSDAL.GetAllShop();
                DataTable shopNoPay = MODEL.Statistics.BillDataTable.CreateShopBillNoPay();
                int status = (int)COM.ShopBillStatus.UnCheckOut;
                double price = 0;
                foreach (Shop shop in ListShop)
                {
                    List<int> ShopBillIds = SSDAL.GetBillIds(shop.ShopId, status, startTime, endTime);
                    if (ShopBillIds == null || ShopBillIds.Count == 0)
                        continue;
                    price = 0;
                    foreach (int billId in ShopBillIds)
                    {
                        price -= bell.GetMoneyCountByBillId(billId);
                    }
                    DataRow row = shopNoPay.NewRow();
                    row["ShopName"] = shop.shopName;
                    row["Price"] = price;
                    shopNoPay.Rows.Add(row);
                }
                return shopNoPay;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 获取由开始时间和结束时间组成的datatable
        /// <summary>
        /// 获取由开始时间和结束时间组成的datatable
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public DataTable GetTimeDT(DateTime startTime, DateTime endTime)
        {
            try
            {
                DataTable dt = MODEL.Statistics.BillDataTable.CreateStartAndEndTime();
                DataRow row = dt.NewRow();
                row["StartTime"] = startTime;
                row["EndTime"] = endTime;
                dt.Rows.Add(row);
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        //--------------------------------------------------------------------------------------------------

        public List<OrderProductEntity> GetOrderProductList(int id)
        {
            List<OrderProductEntity> list = new List<OrderProductEntity>();

            Shop shop = StatisticsDAL.GetShopObj(id);
            if (!shop.Tb_OrderBag.IsLoaded)
            {
                shop.Tb_OrderBag.Load();
            }
            foreach (var item in shop.Tb_OrderBag)
            {

                if (item.Status == (int)OrderBagStatus.Doing)
                {
                    if (!item.orderInfo.IsLoaded)
                    {
                        item.orderInfo.Load();
                    }
                    foreach (var orderProduct in item.orderInfo)
                    {
                        if (orderProduct.status == (int)OrderInfoStatus.NoCheckCount)
                        {
                            OrderProductEntity orderProductEntity = new OrderProductEntity();
                            if (!orderProduct.Tb_OrderBagReference.IsLoaded)
                            {
                                orderProduct.Tb_OrderBagReference.Load();
                            }
                            orderProductEntity.StartOrderTime = orderProduct.Tb_OrderBag.OrderTime;
                            orderProductEntity.CtmName = orderProduct.CtmName;
                            orderProductEntity.OrderProductId = orderProduct.OrderProductId;
                            if (!orderProduct.Tb_ProductReference.IsLoaded)
                            {
                                orderProduct.Tb_ProductReference.Load();
                            }
                            orderProductEntity.OrderProductName = orderProduct.Tb_Product.ProductName;
                            orderProductEntity.PageNum = orderProduct.PageNum;
                            orderProductEntity.PrePriceCount = orderProduct.PrePriceCount == null ? 0 : (decimal)orderProduct.PrePriceCount;
                            orderProductEntity.productNum = orderProduct.productNum;
                            orderProductEntity.statusName = "余额不足";
                            list.Add(orderProductEntity);
                        }
                    }
                }

            }

            return list;
        }

        public Shop GetShopObj(int id)
        {
            return StatisticsDAL.GetShopObj(id);
        }

        public Shop GetShopObj(string name)
        {
            return StatisticsDAL.GetShopObj(name);
        }

        public List<ShopEntity> GetShopData()
        {
            List<ShopEntity> list = new List<ShopEntity>();
            foreach (var item in StatisticsDAL.GetShopObjList())
            {
                ShopEntity shopEntity = new ShopEntity();
                shopEntity.address = item.address;
                shopEntity.Balance = Math.Round(item.Balance == null ? 0 : (decimal)item.Balance, 2);
                shopEntity.OrderBalance = Math.Round(item.OrderBalance == null ? 0 : (decimal)item.OrderBalance, 2);
                shopEntity.CheckOutType = item.CheckOutType == null ? 4 : (int)item.CheckOutType;
                shopEntity.CreditLimit = Math.Round(item.CreditLimit == null ? 0 : (decimal)item.CreditLimit, 2);
                shopEntity.mnemonicCode = item.mnemonicCode;
                shopEntity.ShopId = item.ShopId;
                shopEntity.shopKeeper = item.shopKeeper;
                shopEntity.shopName = item.shopName;
                shopEntity.Tel1 = item.Tel1;
                shopEntity.Tel2 = item.Tel2;
                switch (item.CheckOutType)
                {
                    case 0:
                        shopEntity.CheckOutTypeName = "预付款";
                        break;
                    case 1:
                        shopEntity.CheckOutTypeName = "信用额度";
                        break;
                    case 2:
                        shopEntity.CheckOutTypeName = "现结";
                        break;
                    case 3:
                        shopEntity.CheckOutTypeName = "结账日期";
                        break;
                    case 4:
                        shopEntity.CheckOutTypeName = "结账日期与信用额度";
                        break;
                    default:
                        break;

                }
                list.Add(shopEntity);
            }
            return list;
        }

        public List<Shop> GetShops()
        {
            return StatisticsDAL.GetShopObjList();
        }

        public orderInfo GetOrderInfoObj(int id)
        {
            return StatisticsDAL.GetOrderInfo(id);
        }

        public void Update()
        {
            StatisticsDAL.Update();
        }

        public SHOP_Bill_Info GetBillToView(int inBillID)
        {
            SHOP_Bill_Info sbi = new SHOP_Bill_Info();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                SHOP_Bill shopBill = db.SHOP_Bill.FirstOrDefault(p => p.BillId == inBillID);
                shopBill.ShopReference.Load();
                shopBill.Bill_Detail.Load();
                foreach (Bill_Detail bd in shopBill.Bill_Detail)
                {
                    bd.Bill_Detail_Detail.Load();
                }
                sbi.Shop = new MINERP.MODEL.FoundationModel.Shop_Info() { Address = shopBill.Shop.address, Area = shopBill.Shop.Area, CreditLevel = (int)shopBill.Shop.CreditLevel, CreditLimit = (decimal)shopBill.Shop.CreditLimit, EmailAddr = shopBill.Shop.EmailAddr, IsNeedPrintUPrice = (bool)shopBill.Shop.IsNeedPrintUPrice, MSN = shopBill.Shop.MSN, QQ = shopBill.Shop.QQ, Salesman = shopBill.Shop.Salesman, ShopId = shopBill.Shop.ShopId, ShopName = shopBill.Shop.shopName, ShopKeeper = shopBill.Shop.shopKeeper, Tel1 = shopBill.Shop.Tel1, Tel2 = shopBill.Shop.Tel2 };
                sbi.List_Detail = new List<Bill_Detail_Info>();
                //shopBill.Bill_Detail.ToList().ForEach(
                //    p => { 
                //        sbi.List_Detail.Add(new Bill_Detail_Info(){BillDetailId=p.BillDetailId, CtmName=p.CtmName,

                //            ListDetail_Detail=new List<Bill_Detail_Detail_Info>(),

                //            OrderInfoCode=p.OrderInfoCode, OrderProductName=p.OrderProductName, Remark=p.OiRemark})
                //    }
                //    );
                foreach (Bill_Detail bd in shopBill.Bill_Detail)
                {
                    Bill_Detail_Info bdi = new Bill_Detail_Info();
                    bdi.BillDetailId = bd.BillDetailId;
                    bdi.CtmName = bd.CtmName;
                    bdi.OrderInfoCode = bd.OrderInfoCode;
                    bdi.OrderProductName = bd.OrderProductName;
                    bdi.Remark = bd.OiRemark;
                    bdi.ListDetail_Detail = new List<Bill_Detail_Detail_Info>();
                    foreach (Bill_Detail_Detail bdd in bd.Bill_Detail_Detail)
                    {
                        Bill_Detail_Detail_Info bddi = new Bill_Detail_Detail_Info() { Area = bdd.Area, Count = bdd.Count, DetailName = bdd.DetailName, Num = bdd.Num, Spec = bdd.Spec, Type = (int)bdd.Type, UnitPrice = bdd.UnitPrice, DetailRemark = bdd.DetailRemark };
                        bdi.ListDetail_Detail.Add(bddi);
                    }
                    sbi.List_Detail.Add(bdi);
                }
                sbi.BillCount = (double)shopBill.TotalPrice;
                sbi.ShopBalance = (double)shopBill.ShopBalance;
                sbi.CreateTime = shopBill.CreateTime;
                sbi.BillOrderCode = shopBill.BillOrderCode;
                sbi.BillId = shopBill.BillId;

            }
            return sbi;

        }
    }
}
